PHP: parse $_FILES[] data in multidimesional array
Posted
by
superUntitled
on Stack Overflow
See other posts from Stack Overflow
or by superUntitled
Published on 2010-12-23T06:15:45Z
Indexed on
2010/12/29
3:54 UTC
Read the original article
Hit count: 185
php
|file-upload
Example form here: http://jsfiddle.net/superuntitled/uaTtx/1/
I have a form that allows for dynamic duplication of the form fields. The form allows for file uploads and text input, so the data is sent in both $_POST and $_FILES arrays.
The the initial set of inputs look like this:
<input type="text" name="question[1][text]" />
<input type="file" name="question[1][file]" />
<input type="text" class="a" name="answer[1][text][]" />
<input type="file" name="answer[1][file][]" />
When duplicated the fields are incremented, they look like this:
<input type="text" name="question[2][text]" />
<input type="file" name="question[2][file]" />
<input type="text" class="a" name="answer[2][text][]" />
<input type="file" name="answer[2][file][]" />
To complicate matters, the "answer" form fields can also be duplicated (thus the [] at the end of the 'answer' name array.
How can I parse the posted $_FILES array? I have tried something like this:
foreach ($_FILES['question'] as $p_num)
{
echo $p_num['file']['name'];
foreach ($_FILES['answer'] as $a_num)
{
echo $a_num['file']['name'];
}
}
but I get an "Undefined index: file... " error. How can I parse out the posted values.
© Stack Overflow or respective owner